﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace School.SystemManage.UserManage
{
    public partial class UserList : System.Web.UI.Page
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            var users = from u in db.AdminUsers
                        select new 
                        { 
                            u.UserID,
                            u.UserName,
                            u.UserPwd,
                            u.RealName,
                            u.LoginTime,
                            u.Enabled,
                            u.Role.RoleName
                        };

            rep_Users.DataSource = users.ToList();
            rep_Users.DataBind();
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (RepeaterItem item in rep_Users.Items)
                {
                    CheckBox cb = (CheckBox)item.FindControl("cbChecked");
                    if (cb.Checked)
                    {
                        HiddenField hf = (HiddenField)item.FindControl("hfUid");
                        int uid = int.Parse(hf.Value);
                        var user = db.AdminUsers.First(u => u.UserID == uid);

                        db.DeleteObject(user);
                    }
                }

                db.SaveChanges();
                BindData();
            }
            catch (Exception ex)
            {
                msg.Text = "删除发生异常，原因：" + ex.Message;
            }
            
        }
    }
}
